package net.starwifi.duobao.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import freemarker.template.TemplateException;
import net.starwifi.duobao.utils.FreemarkerUtils;
import org.hibernate.validator.constraints.Length;

import javax.persistence.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2015/8/7.
 */
@Entity
@Table(name = "duob_article")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "duob_article_sequence")
public class Article extends BaseEntity {

    private static final long serialVersionUID = 1475773294701585482L;

    /** 点击数缓存名称 */
    public static final String HITS_CACHE_NAME = "articleHits";

    /** 点击数缓存更新间隔时间 */
    public static final int HITS_CACHE_INTERVAL = 600000;

    /** 内容分页长度 */
    private static final int PAGE_CONTENT_LENGTH = 800;

    /** 内容分页符 */
    private static final String PAGE_BREAK_SEPARATOR = "<hr class=\"pageBreak\" />";

    /** 段落分隔符配比 */
    private static final Pattern PARAGRAPH_SEPARATOR_PATTERN = Pattern.compile("[,;\\.!?，；。！？]");

    /** 静态路径 */
    private static String staticPath;

    /** 单页静态路径 */
    private static String pageStaticPath;


    /** 0校园新闻，1国内外新闻 */
    private Integer type;

    /** 标题 */
    private String title;

    /** 别名*/
    private String alias;

    /** 作者 */
    private String author;

    /** 插图 */
    private String img;

    /* 描述*/
    private String description;

    /** 内容 */
    private String content;

    /** 页面标题 */
    private String seoTitle;

    /** 页面关键词 */
    private String seoKeywords;

    /** 页面描述 */
    private String seoDescription;

    /** 是否发布 */
    private Boolean isPublication;

    /** 是否置顶 */
    private Boolean isTop;

    /** 点击数 */
    private Long hits;

    /** 页码 */
    private Integer pageNumber;

    @JsonProperty
    @Length(max = 200)
    @Column(nullable = false)
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }


    @Column(unique = true,name = "article_alias")
    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }

    @Column
    @JsonProperty
    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Column
    @JsonProperty
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Lob
    @Column
    @JsonProperty
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Column
    @JsonProperty
    public String getSeoTitle() {
        return seoTitle;
    }

    public void setSeoTitle(String seoTitle) {
        this.seoTitle = seoTitle;
    }

    @Column
    public String getSeoKeywords() {
        return seoKeywords;
    }

    public void setSeoKeywords(String seoKeywords) {
        this.seoKeywords = seoKeywords;
    }

    @Column
    public String getSeoDescription() {
        return seoDescription;
    }

    public void setSeoDescription(String seoDescription) {
        this.seoDescription = seoDescription;
    }

    @Column
    public Boolean getIsPublication() {
        return isPublication;
    }

    public void setIsPublication(Boolean isPublication) {
        this.isPublication = isPublication;
    }

    @Column
    @JsonProperty
    public Boolean getIsTop() {
        return isTop;
    }

    public void setIsTop(Boolean isTop) {
        this.isTop = isTop;
    }

    @Column
    public Long getHits() {
        return hits;
    }

    public void setHits(Long hits) {
        this.hits = hits;
    }

    @Column
    public Integer getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(Integer pageNumber) {
        this.pageNumber = pageNumber;
    }

    @Column(name = "article_type")
    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }


    @JsonProperty
    @Column(name = "banner_img")
    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    /**
     * 获取访问路径
     *
     * @return 访问路径
     */
    @Transient
    public String getPath() {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("id", getId());
        model.put("createDate", getCreateDate());
        model.put("modifyDate", getModifyDate());
        model.put("title", getTitle());
        model.put("seoTitle", getSeoTitle());
        model.put("alias",getAlias());
        model.put("seoKeywords", getSeoKeywords());
        model.put("seoDescription", getSeoDescription());
        model.put("pageNumber", getPageNumber());
        try {
            return FreemarkerUtils.process(staticPath, model);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Transient
    public  String getStaticPath(Template template) {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("id", getId());
        model.put("createDate", getCreateDate());
        model.put("modifyDate", getModifyDate());
        model.put("title", getTitle());
        model.put("seoTitle", getSeoTitle());
        model.put("alias",getAlias());
        model.put("seoKeywords", getSeoKeywords());
        model.put("seoDescription", getSeoDescription());
        model.put("pageNumber", getPageNumber());
        try {
            return FreemarkerUtils.process(template.getStaticPath(), model);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
        return null;
    }

}
